package cn.walls1717.wallsmall.order.controller;

import cn.walls1717.wallsmall.order.entity.OrderEntity;
import cn.walls1717.wallsmall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * @author walls1717
 * @since 2022/11/17 17:15
 **/
@Slf4j
@RestController
public class RabbitMQController {
    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMQ")
    public String sendMQ(@RequestParam(value = "num", defaultValue = "10") Integer num) {
        for (int i = 0; i < num; i++) {
            if (i % 2 == 0) {
                OrderReturnReasonEntity reason = new OrderReturnReasonEntity();
                reason.setId(1L);
                reason.setName("123-" + i);
                reason.setSort(0);
                reason.setStatus(0);
                reason.setCreateTime(new Date());

                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", reason,
                    new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息发送完成 -> {}", reason.getName());
            } else {
                OrderEntity order = new OrderEntity();
                order.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello2.java", order,
                    new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息发送完成 -> {}", order.getOrderSn());
            }
        }
        return "ok";
    }
}
